﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;

namespace Web.Shalo.BanPMS1.Admin
{
    public partial class QLLoaiHang : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["manv"] == null || Session["matkhau"] == null)
            {
                Response.Redirect("Default.aspx");
            }
            else
            {
                if (!IsPostBack)
                {
                    HTDSLoaiHang();
                    MultiView1.ActiveViewIndex = 0;
                }
            }

        }
        public void HTDSLoaiHang() // HTDSLoaiHang()
        {
            LoaiHangController obj=new LoaiHangController();
            GVLoaiHang.DataSource = obj.bllDSLoaiHang();
            GVLoaiHang.DataBind();
        }

        protected void btnThemMoiLH_Click(object sender, EventArgs e)
        {
            MultiView1.ActiveViewIndex = 1;
            btnSuaLH.Visible = false;
        }

        protected void btnThemLH_Click(object sender, EventArgs e) //btnThemLH_Click
        {
            LoaiHangController obj = new LoaiHangController();
            bool res = obj.bllThemLH(txtTenlh.Text, txtMota.Text);
            ThongBao(res);
        }

        public void ThongBao(bool res)
        {
            if (res)
            {
                Fresh();
                Response.Redirect("QLLoaiHang.aspx");
            }
            else
            {
                Page.RegisterClientScriptBlock("mes", "<script language='javascript'>alert('Thất Bại')</script>");
            }
        }

        protected void GVLoaiHang_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GVLoaiHang.PageIndex = e.NewPageIndex;
            this.HTDSLoaiHang();
        }

        protected void GVLoaiHang_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            LoaiHangController obj=new LoaiHangController();
            MultiView1.ActiveViewIndex = 1;
            btnThemLH.Visible = false;
            string malh = GVLoaiHang.Rows[e.NewSelectedIndex].Cells[0].Text;
            Session["malh"] = malh;
            txtTenlh.Text = GVLoaiHang.Rows[e.NewSelectedIndex].Cells[1].Text;
            txtMota.Text = GVLoaiHang.Rows[e.NewSelectedIndex].Cells[2].Text;
        }

        protected void btnSuaLH_Click(object sender, EventArgs e)
        {
            bool res;
            LoaiHangController obj = new LoaiHangController();
            res = obj.bllSuaLH(Session["malh"].ToString() , txtTenlh.Text, txtMota.Text);
            ThongBao(res);
           
        }

        protected void GVLoaiHang_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            bool res = false;
            LoaiHangController obj = new LoaiHangController();
            string malh = GVLoaiHang.Rows[e.RowIndex].Cells[0].Text;
            res = obj.bllXoaLH(malh);
            ThongBao(res);
        }

        protected void btnDSLH_Click(object sender, EventArgs e)
        {
            Response.Redirect("QLLoaiHang.aspx");
        }

        public void Fresh()
        {
            txtMota.Text = "";
            txtTenlh.Text = "";
        }
    }
}